home *** CD-ROM | disk | FTP | other *** search
/ CGI How-To / CGI HOW-TO.iso / chap6 / 6_8 / newd_c / array.c next >
Encoding:
C/C++ Source or Header  |  1996-06-15  |  2.5 KB  |  158 lines

  1.  
  2. #include "array.h"
  3.  
  4. #define GROWTH_RATE 2
  5.  
  6.     /* Creates an Array and returns it */
  7.  
  8. Array array_alloc(unsigned int aSize)
  9. {
  10.     Array retVal;
  11.     
  12.     retVal = (Array) malloc(sizeof(struct _array));
  13.     
  14.     retVal->used = 0;
  15.     retVal->size = aSize;
  16.     
  17.     retVal->data = (void **) calloc(aSize,sizeof(void *));
  18.  
  19.     return retVal;
  20. }
  21.  
  22.     /* Frees and array, not the items in it */
  23.  
  24. void array_free(Array anArray)
  25. {
  26.     if(anArray)
  27.     {
  28.         if(anArray->data) free(anArray->data);
  29.     
  30.         free(anArray);
  31.         
  32.         anArray = 0;
  33.     }
  34. }
  35.     
  36.     /* Frees the items, not the array */
  37.  
  38. void array_freeItems(Array anArray)
  39. {
  40.     if(anArray)
  41.     {
  42.         if(anArray->used)
  43.         {
  44.             int i;
  45.             
  46.             for(i=0; i<anArray->used ;i++)
  47.             {
  48.                 if((anArray->data)[i])
  49.                 {
  50.                     free((anArray->data)[i]);
  51.                     (anArray->data)[i] = 0;
  52.                 }
  53.             }
  54.         }
  55.     }
  56. }
  57.  
  58.     /* returns the current size of an Array */
  59.  
  60. int array_count(Array anArray)
  61. {
  62.     int retVal;
  63.     
  64.     if(anArray) retVal = anArray->used;
  65.     else retVal = -1;
  66.     
  67.     return retVal;
  68. }
  69.  
  70.  
  71.     /* Sets the capacity of the array */
  72.  
  73. void array_setCapacity(Array anArray,unsigned int aSize)
  74. {
  75.     if(anArray && (aSize > anArray->size))
  76.     {
  77.         anArray->size = aSize;
  78.         anArray->data = (void **) realloc(anArray->data,aSize*sizeof(void *));
  79.     }
  80. }
  81.  
  82.     /* Adds an item to the end of the array */
  83.  
  84. void array_addItem(Array anArray,void *anItem)
  85. {
  86.     if(anArray) array_addItemAt(anArray,anItem,anArray->used);
  87. }
  88.  
  89.     /* Inserts an item into the array */
  90.  
  91. void array_addItemAt(Array anArray,void *anItem,unsigned int index)
  92. {
  93.     int i;
  94.     
  95.     if(anArray && ((index >= 0) && (index <= anArray->used)) && anItem)
  96.     {
  97.         anArray->used++;
  98.         
  99.         /* Make sure that we have space */
  100.         if(anArray->used >= anArray->size)
  101.         {
  102.             array_setCapacity(anArray,anArray->used * GROWTH_RATE);
  103.         }
  104.         
  105.         /* Move the current items up to make room */
  106.         for(i=(anArray->used - 1); i > index ; i--)
  107.         {
  108.         
  109.             anArray->data[i] = anArray->data[i-1];
  110.         
  111.         }
  112.         
  113.         /* Add the new item */
  114.         anArray->data[i] = anItem;
  115.     }
  116. }
  117.     
  118.     /* Returns an item from the array */
  119.  
  120. void * array_itemAt(Array anArray,unsigned int index)
  121. {
  122.     void *retVal = 0;
  123.     
  124.     if(anArray && ((index >= 0) && (index < anArray->used)))
  125.     {
  126.         retVal = anArray->data[index];
  127.     }
  128.     
  129.     return retVal;
  130. }
  131.  
  132.     /* Removes an item from the array, and returns it */
  133.  
  134. void * array_removeItemAt(Array anArray,unsigned int index)
  135. {
  136.     void *retVal = 0;
  137.     int i;
  138.     
  139.     if((index >= 0) && (index < anArray->used))
  140.     {
  141.         /* get the item to remove */
  142.         
  143.         retVal = anArray->data[index];
  144.         
  145.         /* Move the rest of the items down */
  146.         
  147.         for(i=index; i < anArray->used - 1;i++)
  148.         {
  149.             anArray->data[i] = anArray->data[i+1];
  150.         }
  151.         
  152.         anArray->used--;
  153.     }
  154.  
  155.     return retVal;
  156. }
  157.  
  158.